﻿using System.ComponentModel.Composition;
using AppsGenerator.Generators;
using DBBuilder.DBDataStructure;
using AppsGenerator.Utils;

namespace AppsGenerator.WebGenerators.WebViewGenerator
{
    /// <summary>
    /// The base class for generating partials of view
    /// </summary>
    [Export(typeof(PartialViewGenerator))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class PartialViewGenerator : BaseGenerator
    {
        /// <summary>
        /// The variable presents the algorithm to generate list view partial
        /// </summary>
        private IListViewItemAlgorithm _listAlgorithm;

        /// <summary>
        /// The variable presents the algorithm to generate insert view partial
        /// </summary>
        private IInsertViewItemAlgorithm _insertAlgorithm;

        /// <summary>
        /// The variable presents the algorithm to generate edit view partial
        /// </summary>
        private IEditViewItemAlgorithm _editAlgorithm;

        /// <summary>
        /// The variable presents the algorithm to generate delete view partial
        /// </summary>
        private IDeleteViewItemAlgorithm _deleteAlgorithm;

        /// <summary>
        /// The variable presents the algorithm to generate detail view partial
        /// </summary>
        private IDetailViewItemAlgorithm _detailAlgorithm;


        /// <summary>
        /// Method for generating contents of controller components and save generated contents to a file
        /// </summary>
        /// <param name="tbl">The data source is corresponding with class which will be generated</param>
        public void GenerateContents(Table tbl)
        {
            string imgAttr;
            string fckAttr;

            if (tbl.IsTableWithImage(out imgAttr) && tbl.IsTableWithFCK(out fckAttr))
            {
                this._listAlgorithm = IoC.Get<ListViewItemByImageAndFCKAlgorithm>();
                this._insertAlgorithm = IoC.Get<InsertViewItemByImageAndFCKAlgorithm>();
                this._deleteAlgorithm = IoC.Get<DeleteViewItemByCommonAlgorithm>();
                this._detailAlgorithm = IoC.Get<DetailViewItemByImageAndFCKAlgorithm>();
            }
            else if (tbl.IsTableWithImage(out imgAttr))
            {
                this._listAlgorithm = IoC.Get<ListViewItemByImageAlgorithm>();
                this._insertAlgorithm = IoC.Get<InsertViewItemByImageAlgorithm>();
                this._deleteAlgorithm = IoC.Get<DeleteViewItemByCommonAlgorithm>();
                this._detailAlgorithm = IoC.Get<DetailViewItemByImageAlgorithm>();
            }
            else if (tbl.IsTableWithFCK(out fckAttr))
            {
                this._listAlgorithm = IoC.Get<ListViewItemByFCKAlgorithm>();
                this._insertAlgorithm = IoC.Get<InsertViewItemByFCKAlgorithm>();
                this._deleteAlgorithm = IoC.Get<DeleteViewItemByCommonAlgorithm>();
                this._detailAlgorithm = IoC.Get<DetailViewItemByFCKAlgorithm>();
            }
            else if (tbl.IsUserTable())
            {
                this._listAlgorithm = IoC.Get<ListViewItemByUserAlgorithm>();
                this._insertAlgorithm = IoC.Get<InsertViewItemByUserAlgorithm>();
                this._editAlgorithm = IoC.Get<EditViewItemByUserAlgorithm>();
                this._deleteAlgorithm = IoC.Get<DeleteViewItemByUserAlgorithm>();
                this._detailAlgorithm = IoC.Get<DetailViewItemByUserAlgorithm>();
            }
            else
            {
                this._listAlgorithm = IoC.Get<ListViewItemByCommonAlgorithm>();
                this._insertAlgorithm = IoC.Get<InsertViewItemByCommonAlgorithm>();
                this._editAlgorithm = IoC.Get<EditViewItemByCommonAlgorithm>();
                this._deleteAlgorithm = IoC.Get<DeleteViewItemByCommonAlgorithm>();
            }


            // Generate List view partial
            if (this._listAlgorithm != null)
            {
                this._listAlgorithm.GenerateContents(tbl);
            }

            // Generate Insert view partial
            if (this._insertAlgorithm != null)
            {
                this._insertAlgorithm.GenerateContents(tbl);
            }

            // Generate Edit view partial
            if (this._editAlgorithm != null)
            {
                this._editAlgorithm.GenerateContents(tbl);
            }

            // Generate Edit view partial
            if (this._deleteAlgorithm != null)
            {
                this._deleteAlgorithm.GenerateContents(tbl);
            }

            // Generate Detail view partial
            if (this._detailAlgorithm != null)
            {
                this._detailAlgorithm.GenerateContents(tbl);
            }
        }


    }
}
